<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:import url="Menu.jsp"></c:import>	
<table border="1" style="width: 100%; height: 100%">
	<tbody>
		<tr>
			<th colspan="2" height="30">Student Management System</th>
		</tr>
		<tr>
			<td valign="top">Welcome, ${use.fname } ${sessionScope.use.lname }<br />
				<c:choose>
					<c:when test="${empty sessionScope.students }">
						<em>Sorry no student found with the name like: ${param.key }</em>
					</c:when>
					<c:otherwise>
						<c:if test="${empty param.pageNo }" var="flag">
							<c:set var="perPageRecords" value="2" scope="session">
							<c:set var="pageNo" value="1"></c:set>				
							</c:set>
						</c:if>
						<c:if test="${not flag }">
							<c:set var="pageNo" value="${param.pageNo }"></c:set>
						</c:if>
						<c:set var="begin" value="${pageNo-1 * perPageRecords }">
							<c:forEach items="${students }" var="student" begin="${begin }" end="${begin+(perPageRecords-1) }">
							</c:forEach>
						</c:set>
						<table>
							<tbody>
								<tr>
									<th>SID</th>
									<th>First Name</th>
									<th>Last Name</th>
								</tr>
								<tr bgcolor="#00ffff">
									<td>${student.sid }</td>
									<td>${student.fname }</td>
									<td>${student.lname }</td>
									<td>
										<c:url var="searchStudentUrl" value="searchStudent">
											<c:param name="criteria" value="SID">
												<c:param name="key" value="${student.sid }"></c:param>
											</c:param>
										</c:url>
										<a href="${searchStudentUrl }">More Details</a>
									</td>
								</tr>
								<tr>
									<td colspan="4">
									<strong>Page No: ${pageNo }</strong> |
									<c:if test="${pageNo != 1 }">
										<c:url var="previousUrl" value="MultiStudentDetails.jsp">
											<c:param name="pageNo" value="${pageNo-1 }"></c:param>
											
										</c:url>
										<a href="$previousUrl">Previous</a>
									</c:if>
									
									<c:if test="${fn:length(students) > (begin + (perPageRecords-1))}">
										<c:url var="nextUrl" value="MultiStudentDetail.jsp">
											<c:param name="pageNo" value="${pageNo+1 }"></c:param>
										</c:url>
										<a href="nextUrl">Next</a>		
									</c:if>
									</td>
								</tr>
							</tbody>
						</table>
					</c:otherwise>
				</c:choose>
			</td>
		</tr>
		<tr>
			<th colspan="2" height="30">@ Copyright Trần Thanh I Len</th>
		</tr>
	</tbody>
</table>